#include "buffer.h"

namespace TinyRPC{


std::string Buffer::read(){
    int old_readIndex = m_readIndex;
    
    std::string str =  std::string(&m_buf[m_readIndex],m_writeIndex - m_readIndex);

    m_buf.erase(m_buf.begin(), m_buf.begin()+m_readIndex);
    m_readIndex = m_writeIndex = 0;
    return str;
}

void Buffer::append(char* str, int strlen){
    m_writeIndex += strlen;
    m_buf.reserve(m_writeIndex);
    for(int i=0;i<strlen;i++){
        m_buf.push_back(str[i]);
    }
}
void Buffer::append(const std::string& str){
    m_writeIndex += str.size();
    m_buf.reserve(m_writeIndex);
    for(int i=0;i<str.size();i++){
        m_buf.push_back(str[i]);
    }
}
char* Buffer::charAt(int idx){
    return &m_buf[idx];
}
int Buffer::size(){
    return m_buf.size();
}


Buffer::Buffer()
: m_writeIndex(0)
, m_readIndex(0){

}
}